/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.crud.controller;

import java.util.List;

import javax.swing.table.TableModel;
import systole.persistence.FacadeDB;
import systole.view.abstractController.AbstractViewController;
import systole.view.crud.JDialogEntities;
import systole.view.crud.tableModels.SystoleTableModel;
import systole.view.utils.ErrorDialog;

/**
 * 
 * @author jmj
 */
public abstract class ControllerEntityView extends AbstractViewController {

    /**
     * Form to list the entities
     */
    protected JDialogEntities entities;
    /**
     * Edition controller to current entity class
     */
    protected ControllerEntityEdition controllerEntityEdition;
    /**
     * Table model to current entity class
     */
    protected SystoleTableModel tableModel;
    /**
     * Entity name
     */
    protected String entityName;
    /**
     * Plural entity name
     */
    protected String pluralEntity;
    /**
     * Facade to DB access
     */
    protected FacadeDB facadeDB;
    /**
     * Entities list
     */
    @SuppressWarnings("unchecked")
    protected List entityList;
    /**
     * Selected entity
     */
    protected Object selectedEntity = null;

    /**
     */
    public ControllerEntityView() {
        super();
        this.facadeDB = FacadeDB.getInstance();
    }

    private void initForm(java.awt.Frame parent) {
        this.entities = new JDialogEntities(parent, this);
        this.entities.setTitle("Administración de " + this.pluralEntity);
        this.entities.getJXEntityHeader().setTitle(this.pluralEntity);
        this.entities.getJXEntityHeader().setDescription("Gestión de " + this.pluralEntity);
        this.entities.getjBtnEdit().setToolTipText("Editar " + this.entityName);
        this.entities.getjBtnNew().setToolTipText("Nuevo " + this.entityName);
        this.entities.getjBtnRemove().setToolTipText(
                "Eliminar " + this.entityName);
        this.entities.getjBtnSelect().setToolTipText("Seleccionar " + this.entityName);
        this.entities.setLocationRelativeTo(parent);
        this.entities.getjTableEntities().packAll();
        this.setCloseListener(entities);
        this.loadIconOnForm();
    }

    /**
     * @param parent
     */
    public void showForm(java.awt.Frame parent) {
        this.initForm(parent);
        this.entities.getjBtnRemove().setVisible(this.canDeleteEntity());
        this.entities.getjBtnSelect().setVisible(false);        
        this.entities.setVisible(true);
    }

    /**
     * @param paren
     */
    public void showFormToSelect(java.awt.Frame paren) {
        this.initForm(paren);
        this.entities.getjBtnRemove().setVisible(false);
        this.entities.setVisible(true);

    }

    /**
     * edit the entity selected
     */
    public void editEntity() {
        int selected = this.entities.getjTableEntities().getSelectedRow();
        if (selected != -1) {
            this.controllerEntityEdition.editEntity(this.entityList.get(selected));
            this.selectedEntity = this.entityList.get(selected);
            this.refreshList();
        }
    }

    /**
     * create a new entity
     */
    public void newEntity() {
        this.controllerEntityEdition.newEntity();
        this.refreshList();

    }

    /**
     * delete the entity selected
     */
    public void removeEntity() {
        // no se remueven entidades por el momento
    }

    /**
     * select an entity from table
     */
    public void selectEntity() {
        int selected = this.entities.getjTableEntities().getSelectedRow();
        if (selected != -1) {
            this.selectedEntity = this.entityList.get(selected);
            this.closeForm();
        } else {
             ErrorDialog.showError(this.entities,this.entityName + " no seleccionado");
        }
    }

    /**
     * @return the table model
     *
     */
    public TableModel getEntityTableModel() {
        return this.tableModel;
    }

    /**
     * Method to close form
     */
    public void closeForm() {
        this.entities.setVisible(false);
        this.entities.dispose();
    }

    /**
     * @return the selected object
     */
    public Object getSelectedEntity() {
        return this.selectedEntity;
    }

    /**
     * Method to reload the entity list
     */
    public abstract void refreshList();
    
    protected abstract boolean canDeleteEntity();

    protected abstract void loadIconOnForm();


}
